import os
from threading import Thread

from my_json import load_json, save_json, download_json, bulid_download_path
from spider_url import school_grade_sim_province_year_json_url, years

basic_path = str(os.path.dirname(__file__)) + "/"
school_id_path = basic_path + "../basic_info/school_id.json"
province_id_path = basic_path + "../basic_info/province_id.json"
download_path = bulid_download_path(basic_path)


def init():
    url = school_grade_sim_province_year_json_url()
    school_id_list = load_json(school_id_path)
    school_id_list = list(set(school_id_list.keys()))
    school_id_list.sort()
    province_id_list = load_json(province_id_path)
    province_id_list = list(set(province_id_list.keys()))
    province_id_list.sort()
    thread_list = []
    for province_id in province_id_list:
        t = Thread(target=school_province, args=(province_id, school_id_list, url))
        thread_list.append(t)
        t.start()
    for t in thread_list:
        t.join()


def school_province(province_id, school_id_list, url):
    ds = {}
    for school_id in school_id_list:
        ds[school_id] = {}
        for year in years:
            try:
                school0 = download_json(url.to_string(school_id, province_id, year),
                                        download_path + "school_" + province_id + "_" + school_id + "_" + year + ".json")
            except ValueError as e:
                print(e)
                print("ValueError" + province_id + school_id + year)
                continue
            ds[school_id][year] = school0["data"]
    save_json(basic_path + "school_grade_sim" + province_id + ".json", ds)
